package com.ygq.seckill.service.impl;


import com.ygq.seckill.entity.User;
//import com.ygq.seckill.exception.GlobalException;
import com.ygq.seckill.exception.GlobalException;
import com.ygq.seckill.mapper.UserMapper;
import com.ygq.seckill.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ygq.seckill.util.CookieUtil;
import com.ygq.seckill.util.MD5Util;
import com.ygq.seckill.util.UUIDUtil;
import com.ygq.seckill.vo.LoginVo;
import com.ygq.seckill.vo.RespBean;
import com.ygq.seckill.vo.RespBeanEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 姚国庆
 * @since 2024-06-03
 */
@Service

public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private RedisTemplate redisTemplate;
    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        //在表里，主键id即为电话号
        User user = userMapper.selectById(mobile);
        if (user == null) {
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
           // throw new GlobalException(RespBeanEnum.LOGIN_ERROR);//抛异常让全局处理
        }
        if (!MD5Util.midPassToDBPass(password, user.getSlat()). equals(user.getPassword())) {
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }
        //唯一的uuid(ticket)
        String ticket = UUIDUtil.uuid();
        //将登录的用户信息 保存到session
//        request.getSession().setAttribute(ticket, user);

        //为了实现分布式Session,将用户信息放入redis中
       redisTemplate.opsForValue().set("user:" + ticket, user);

        CookieUtil.setCookie(request, response, "userTicket", ticket);
        return RespBean.success(ticket);
    }

    @Override
    public User getUserByCookie(String userTicket, HttpServletRequest request, HttpServletResponse response) {
        if(!StringUtils.hasText(userTicket)){
            return null;
        }
        //根据userTicket到Redis获取user'
        User user = (User)redisTemplate.opsForValue().get("user:" + userTicket);
        if(user!=null){
            //相当于刷新了一次cookie,生命周期flush
            CookieUtil.setCookie(request, response, "userTicket", userTicket);
        }
        return user;
    }

    @Override
    public RespBean updatePassword(String userTicket,
                                   String password,
                                   HttpServletRequest request,
                                   HttpServletResponse response) {
        User user = getUserByCookie(userTicket, request, response);
        if(user==null){
            throw new GlobalException(RespBeanEnum.MOBILE_NO_EXIST);
        }
        User user1 = user.setPassword(MD5Util.inputPassToDBPass(password,user.getSlat()));
        int affect=userMapper.updateById(user1);
        if(affect==1){
            //删除redis对应的数据
            redisTemplate.delete("user:"+userTicket);
            return RespBean.success();
        }
        return RespBean.error(RespBeanEnum.UPDATE_PASSWORD_FAIL);
    }

}
